<template>
  <el-dialog
    :title="!dataForm.gsmBargainRecordId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="usmId">
      <el-input v-model="dataForm.usmId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="" prop="gsmBargainId">
      <el-input v-model="dataForm.gsmBargainId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="gsmCreateTime">
      <el-input v-model="dataForm.gsmCreateTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="psmPrice">
      <el-input v-model="dataForm.psmPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="当前价格" prop="gsmBargainPriceNow">
      <el-input v-model="dataForm.gsmBargainPriceNow" placeholder="当前价格"></el-input>
    </el-form-item>
    <el-form-item label="当前砍价次数" prop="gsmBargainCountNow">
      <el-input v-model="dataForm.gsmBargainCountNow" placeholder="当前砍价次数"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="psmName">
      <el-input v-model="dataForm.psmName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品展示" prop="psmImg">
      <el-input v-model="dataForm.psmImg" placeholder="商品展示"></el-input>
    </el-form-item>
    <el-form-item label="商品标题" prop="psmTitle">
      <el-input v-model="dataForm.psmTitle" placeholder="商品标题"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则" prop="gsmRuleType">
      <el-input v-model="dataForm.gsmRuleType" placeholder="砍价规则"></el-input>
    </el-form-item>
    <el-form-item label="当前状态" prop="gsmBargainRecordStatus">
      <el-input v-model="dataForm.gsmBargainRecordStatus" placeholder="当前状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gsmBargainRecordId: 0,
          usmId: '',
          gsmBargainId: '',
          gsmCreateTime: '',
          psmPrice: '',
          gsmBargainPriceNow: '',
          gsmBargainCountNow: '',
          psmName: '',
          psmImg: '',
          psmTitle: '',
          gsmRuleType: '',
          gsmBargainRecordStatus: ''
        },
        dataRule: {
          usmId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          gsmBargainId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          gsmCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          psmPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          gsmBargainPriceNow: [
            { required: true, message: '当前价格不能为空', trigger: 'blur' }
          ],
          gsmBargainCountNow: [
            { required: true, message: '当前砍价次数不能为空', trigger: 'blur' }
          ],
          psmName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          psmImg: [
            { required: true, message: '商品展示不能为空', trigger: 'blur' }
          ],
          psmTitle: [
            { required: true, message: '商品标题不能为空', trigger: 'blur' }
          ],
          gsmRuleType: [
            { required: true, message: '砍价规则不能为空', trigger: 'blur' }
          ],
          gsmBargainRecordStatus: [
            { required: true, message: '当前状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gsmBargainRecordId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmBargainRecordId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainrecord/info/${this.dataForm.gsmBargainRecordId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.usmId = data.bargainRecord.usmId
                this.dataForm.gsmBargainId = data.bargainRecord.gsmBargainId
                this.dataForm.gsmCreateTime = data.bargainRecord.gsmCreateTime
                this.dataForm.psmPrice = data.bargainRecord.psmPrice
                this.dataForm.gsmBargainPriceNow = data.bargainRecord.gsmBargainPriceNow
                this.dataForm.gsmBargainCountNow = data.bargainRecord.gsmBargainCountNow
                this.dataForm.psmName = data.bargainRecord.psmName
                this.dataForm.psmImg = data.bargainRecord.psmImg
                this.dataForm.psmTitle = data.bargainRecord.psmTitle
                this.dataForm.gsmRuleType = data.bargainRecord.gsmRuleType
                this.dataForm.gsmBargainRecordStatus = data.bargainRecord.gsmBargainRecordStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainrecord/${!this.dataForm.gsmBargainRecordId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmBargainRecordId': this.dataForm.gsmBargainRecordId || undefined,
                'usmId': this.dataForm.usmId,
                'gsmBargainId': this.dataForm.gsmBargainId,
                'gsmCreateTime': this.dataForm.gsmCreateTime,
                'psmPrice': this.dataForm.psmPrice,
                'gsmBargainPriceNow': this.dataForm.gsmBargainPriceNow,
                'gsmBargainCountNow': this.dataForm.gsmBargainCountNow,
                'psmName': this.dataForm.psmName,
                'psmImg': this.dataForm.psmImg,
                'psmTitle': this.dataForm.psmTitle,
                'gsmRuleType': this.dataForm.gsmRuleType,
                'gsmBargainRecordStatus': this.dataForm.gsmBargainRecordStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
